<!-- TrainMineAroundStudents.html -->
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
	<title>周边同学</title>
	<link rel="stylesheet" href="../../css/base.css">
</head>
<body>
   <div class="base_style_color " id='BarAppearance' style="height: 22px;width: 100%"></div> 
	<div class="base_navigation_bar base_style_color base_navigation_item_bar">
		<a class="base_left_item" href="#" onclick="backClick()">
			<img class="base_back_icon" src="../../image/icon_jiantou.png" alt="">
		</a>
		<span>周边同学</span>
		<a class="base_right_item base_hide_item">
		</a>
	</div>
</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/jquery.min.js"></script>
<script type="text/javascript" src="../../script/MD5.js"></script>
<script type="text/javascript" src="../../script/requestTool.js"></script>
<script type="text/javascript">
	var map;
	var studentDatas = new Array();
	// var studentLocals = [
	// 						{
	// 							id : 0,
	// 							lon : 116.46834,
	// 							lat : 39.884815
	// 						},{
	// 							id : 1,
	// 							lon : 116.76834,
	// 							lat : 39.880295
	// 						},{
	// 							id : 2,
	// 							lon : 116.46034,
	// 							lat : 39.804815
	// 						},{
	// 							id : 3,
	// 							lon : 116.46234,
	// 							lat : 39.084815
	// 						},{
	// 							id : 4,
	// 							lon : 116.06834,
	// 							lat : 39.811815
	// 						},{
	// 							id : 5,
	// 							lon : 116.49834,
	// 							lat : 39.880015
	// 						},{
	// 							id : 6,
	// 							lon : 116.66834,
	// 							lat : 39.284815
	// 						},{
	// 							id : 7,
	// 							lon : 116.46034,
	// 							lat : 39.194815
	// 						},{
	// 							id : 8,
	// 							lon : 116.46234,
	// 							lat : 39.888815
	// 						}
	// 					];
	apiready = function(){
	    setupMap();
	}
	function loadData(lat, lon){
		cfnetppPOST(urlPeriphery, 
	    {
	      uid: $api.getStorage('cfnetppuid'),
	      jid: $api.getStorage('cfnetppjid'),
	      lat: lat,
	      lon: lon
	    },
	    false, 
	    function(data,status){
	    	if (status == 'success') {
	    		if (data.status == 1) {
	    			var jsonData = data.json;
	    			studentDatas = jsonData;
	    			var studentLocals = new Array();
	    			for (var i = 0; i < jsonData.length; i++) {
	    				studentData = jsonData[i];
	    				var annotation = {
	    					id: i,
	    					lon: studentData.lon,
	    					lat: studentData.lat
	    				};
	    				studentLocals.push(annotation);
	    			}
	    			setupAroundStudents(studentLocals);
	    		}else{
	    			api.toast({
	    			    msg: '获取周边同学信息失败'
	    			});
	    		}
	    	}else{
	    		api.toast({
	    		    msg: '获取周边同学信息失败'
	    		});
	    	}
	    });
	}
	function setupMap(){
		var y = $('.base_navigation_bar').height();
		var h = api.winHeight - y;
		var w = api.winWidth;
		map = api.require('bMap');
		map.getLocation({
		    accuracy: '100m',//定位精度
		    autoStop: true,//获取到位置信息后是否自动停止定位
		    filter: 1//位置更新所需的最小距离（单位米），autoStop 为 true 时，此参数有效
		}, function(ret, err){
			var nowlat = ret.lat;
			var nowlon = ret.lon;
		    if(ret.status){
		    	map.open({
				    rect: {
				        x: 0,
				        y: y,
				        w: w,
				        h: h
				    },
				    center: {
				        lon: nowlon,
				        lat: nowlat
				    },
				    zoomLevel: 16,//设置百度地图缩放等级，取值范围：3-18级
				    showUserLocation: true,//是否在地图上显示用户位置
				    fixedOn: api.frameName,
				    fixed: true//模块是否随所属 window 或 frame 滚动 true不随之滚动
				}, function(ret){
				    if(ret.status){
				    	loadData(nowlat,nowlon);
				    }
				});
		    }else{
		        // alert(err.code);
		        api.toast({
		            msg: '打开地图失败，请于系统设置中开启该app的定位权限！'
		        });
		    }
		});   
	}
	//设置周边同学
	function setupAroundStudents(studentLocals){
		map.addAnnotations({
		    annotations: studentLocals
		}, function(ret){
		    if(ret){
		    	var index = parseInt(ret.id);
		    	var studentData = studentDatas[index];
		    	api.openWin({useWKWebView:true,
	                name: 'PhoneUsage',
	                url: 'PhoneUsage.html',
	                vScrollBarEnabled: true,
	                hScrollBarEnabled: false,
	                pageParam : {
	                    data : {
	                      	uid: studentData.uid,
		                    jid: studentData.jid
		                }
	                }
	            });
		    }
		});
	}
	function backClick(){
		api.closeWin();
	}
	    
</script>
</html>